package cn.iocoder.yudao.module.blog.service.askresponse;

import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.askresponse.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.askresponse.BlogAskResponseDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.askresponse.BlogAskResponseConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.askresponse.BlogAskResponseMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 学员问题回复 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogAskResponseServiceImpl implements BlogAskResponseService {

    @Resource
    private BlogAskResponseMapper askResponseMapper;

    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public Long createAskResponse(BlogAskResponseCreateReqVO createReqVO) {
        // 插入
        BlogAskResponseDO askResponse = BlogAskResponseConvert.INSTANCE.convert(createReqVO);

        Long id = getLoginUserId();
        if(id == null){
            throw exception(ASK_USER_NOT_LOGIN);
        }

        MemberUserRespDTO userRespDTO = memberUserApi.getUser(id);
        if(userRespDTO == null){
            throw exception(ASK_USER_NOT_FOUND);
        }

        askResponse.setAvatar(userRespDTO.getAvatar());
        askResponse.setMobile(userRespDTO.getMobile());
        askResponse.setNickname(userRespDTO.getNickname());
        askResponse.setMemberId(userRespDTO.getId());

        askResponseMapper.insert(askResponse);
        // 返回
        return askResponse.getId();
    }

    @Override
    public void updateAskResponse(BlogAskResponseUpdateReqVO updateReqVO) {
        // 校验存在
        validateAskResponseExists(updateReqVO.getId());
        // 更新
        BlogAskResponseDO updateObj = BlogAskResponseConvert.INSTANCE.convert(updateReqVO);
        askResponseMapper.updateById(updateObj);
    }

    @Override
    public void deleteAskResponse(Long id) {
        // 校验存在
        validateAskResponseExists(id);
        // 删除
        askResponseMapper.deleteById(id);
    }

    private void validateAskResponseExists(Long id) {
        if (askResponseMapper.selectById(id) == null) {
            throw exception(ASK_RESPONSE_NOT_EXISTS);
        }
    }

    @Override
    public BlogAskResponseDO getAskResponse(Long id) {
        return askResponseMapper.selectById(id);
    }

    @Override
    public List<BlogAskResponseDO> getAskResponseList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return askResponseMapper.selectBatchIds(ids);
    }

    @Override
    public List<BlogAskResponseDO> getAskResponseListByAskIds(Collection<Long> askIds){
        if(CollUtil.isEmpty(askIds)){
            return ListUtil.empty();
        }

        List<BlogAskResponseDO> list = askResponseMapper.selectList(BlogAskResponseDO::getAskId, askIds);
        return list;
    }

    @Override
    public List<BlogAskResponseDO> getAskResponseListByAskId(Long askId){
        return askResponseMapper.selectList(BlogAskResponseDO::getAskId, askId);
    }

    @Override
    public PageResult<BlogAskResponseDO> getAskResponsePage(BlogAskResponsePageReqVO pageReqVO) {
        return askResponseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogAskResponseDO> getAskResponseList(BlogAskResponseExportReqVO exportReqVO) {
        return askResponseMapper.selectList(exportReqVO);
    }

}
